package com.quan.upms.web.controller;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.quan.base.common.base.web.support.BaseResult;
import com.quan.base.common.base.web.support.controller.BaseController;
import com.quan.upms.annotation.RequiresPermissions;
import com.quan.upms.rpc.api.UpmsPermissionService;
import com.quan.upms.rpc.extend.UpmsPermissionDto;
import com.quan.upms.rpc.model.UpmsPermission;

@RestController
@RequestMapping(value = "/api/upms/permission")
public class UpmsPermissionController extends BaseController {

	@Autowired
	private UpmsPermissionService upmsPermissionService;

	@RequiresPermissions(value="upms:permission:add", description="添加权限")
	@PostMapping(value = "/add")
	public Object add(@Valid @RequestBody UpmsPermission entity, BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
            return BaseResult.failure(bindingResult.getFieldError().getDefaultMessage()) ;
        }
		if (null != this.upmsPermissionService.checkValue("code", entity.getCode())) {
			return BaseResult.failure("编码已存在");
		}
		this.upmsPermissionService.insertSelective(entity);
		return BaseResult.ok();
	}

	@RequiresPermissions(value="upms:permission:delete", description="删除权限")
	@GetMapping(value = "/delete/{id}")
	public Object delete(@PathVariable Integer id) {
		this.upmsPermissionService.delete(id);
		return BaseResult.ok();
	}
	
	@RequiresPermissions(value="upms:permission:delete", description="删除权限")
	@GetMapping(value = "/deleteBatch")
	public Object deleteBatch(String ids) {
		this.upmsPermissionService.deleteByIds(ids) ;
		return BaseResult.ok();
	}

	@RequiresPermissions(value="upms:permission:update", description="修改权限")
	@PostMapping(value = "/update")
	public Object update(@Valid @RequestBody UpmsPermission entity, BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
            return BaseResult.failure(bindingResult.getFieldError().getDefaultMessage()) ;
        }
		UpmsPermission oldPermission = this.upmsPermissionService.selectById(entity.getId()) ;
		if(null != oldPermission.getCode() && !oldPermission.getCode().equals(entity.getCode())) {
			if(null != this.upmsPermissionService.checkValue("code", entity.getCode())) {
				return BaseResult.failure("编码已存在");
			}
		}
		this.upmsPermissionService.updateSelective(entity);
		return BaseResult.ok();
	}

	@RequiresPermissions(value="upms:permission:detail", description="查询权限详情")
	@GetMapping(value = "/get/{id}")
	public Object get(@PathVariable Integer id) {
		return this.upmsPermissionService.selectById(id);
	}

	@RequiresPermissions(value="upms:permission:list", description="查询权限")
	@GetMapping(value = "/list")
	public Object list(UpmsPermissionDto dto) {
		return this.upmsPermissionService.selectList(dto, null) ;
	}
	
	@GetMapping(value = "/tree")
	public Object tree(UpmsPermission entity) {
		return this.upmsPermissionService.tree(entity);
	}
}
